<?php
App::uses('AppController', 'Controller');
class OnlineController extends AppController{
    public $uses = array('User','UserCat','Category');
    function beforeFilter(){ 
        parent::beforeFilter();
        $this->layout = 'general';
    }
    function index(){
        $cmd = '';
        $idCat = 0;
        $cmd = @$this->request->query['cmd'];
        $idCat = @$this->request->query['idCat'];
        $arrDataUserReturn = array();
        $next = 0;
        $arrDataUserInfo = array();
        $payType = -1;
        switch ($cmd){
            case 'new':
                //  create_time 
                $arrReturn = $this->newData($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'hr':
                //user_cat and user
                $payType = 2;
                $arrReturn = $this->getPayHr($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'turn':
                //user_cat and user
                $payType = 1;
                $arrReturn = $this->getPayTurn($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'free':
                //user_cat and user
                $arrReturn = $this->getPayFree($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                $payType = 0;
                break;
            case 'save':
                //total_save
                $arrReturn = $this->totalSave($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            default:
                //start and total vote
                $arrReturn = $this->getDefault($idCat);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
        }
        $arrCategory = $this->getCategory();
        $urlNext = '/Online/index';
        if($cmd){
            $urlNext .= '?cmd='.$cmd;
        }else{
            $urlNext .= '?t=1';
        }
        $this->set('arrCategory',$arrCategory);
        $arrDataUserReturn = $this->buildDatZend($arrDataUserInfo,$payType);
        $this->set('urlNext',$urlNext);
        $this->set('baseUrl',$this->base);
        $this->set('arrDataUserReturn',$arrDataUserReturn[0]);
        $this->set('arrCatName',$arrDataUserReturn[1]);
        $this->set('next',$next);
        $this->set('cmd',$cmd);
    }
    function runDataAjax(){
        $typeFiter  = @$this->request->data['typeFiter'];
        $page_no    = @$this->request->data['page_no'];
        $idCat      = @$this->request->data['idCat'];
        $ofset      = ($page_no - 1).AppConst::PER_PAGE_HOME;
        switch ($typeFiter){
            case 'new':
                //  create_time 
                $arrReturn = $this->newData($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'hr':
                //user_cat and user
                $payType = 2;
                $arrReturn = $this->getPayHr($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'turn':
                //user_cat and user
                $payType = 1;
                $arrReturn = $this->getPayTurn($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            case 'free':
                //user_cat and user
                $arrReturn = $this->getPayFree($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                $payType = 0;
                break;
            case 'save':
                //total_save
                $arrReturn = $this->totalSave($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
            default:
                //start and total vote
                $arrReturn = $this->getDefault($idCat,$ofset);
                $arrDataUserInfo = $arrReturn['userInfo'];
                $next = $arrReturn['next'];
                break;
        }
        $arrDataUserReturn = $this->buildDatZend($arrDataUserInfo,$payType);
        if(!empty($arrDataUserReturn)){
            $dataResult = array('code' => 1,'html' => $this->zendHtml($arrDataUserReturn));
            $arrDataReturn = json_encode($dataResult);
        }else{
            $arrDataReturn = json_encode(array('code' => 0));
        }
		$this->response->body($arrDataReturn);
		return $this->response;
    }
    function buildDatZend($arrDataUserInfo = array(),$payType=-1){
        $arrDataUserReturn = array();
        $arrCatName = array();
        $arrUCatId = array();
        if(!empty($arrDataUserInfo)){
            $arrUserId = array();
            $arrCatInfo = array();
            $arrUserCat = array();
            foreach($arrDataUserInfo AS $itemUser){
                $arrUserId[]    =   $itemUser['User']['id'];
            }
            if(!empty($arrUserId)){
                if($payType == -1){
                    $arrUserCat = $this->UserCat->getDataByCondition('*',array('user_id' => $arrUserId));
                }
                else{
                    $arrUserCat = $this->UserCat->getDataByCondition('*',array('user_id' => $arrUserId,'type' => $payType));
                }
                $arrCat = array();
                if(!empty($arrUserCat)){
                    foreach($arrUserCat AS $itemUC){
                        $arrCat[] = $itemUC['UserCat']['cat_id'];
                        $arrUCatId[$itemUC['UserCat']['user_id']][] = $itemUC['UserCat']['cat_id'];
                    }
                }
                if(!empty($arrCat)){
                    $arrCatInfo = $this->Category->getDataByCondition('*',array('id' => $arrCat));
                    if(!empty($arrCatInfo)){
                        foreach($arrCatInfo AS $itemCat){
                            $arrCatName[$itemCat['Category']['id']] =   $itemCat['Category']['category_name'];
                        }
                    }
                }
            }
            foreach($arrDataUserInfo AS $itemUser){
                $star = 0;
                if($itemUser['User']['total_rate'] != 0)
                    $star = round($itemUser['User']['total_star']/$itemUser['User']['total_rate'],0);
                if($star == 0){
                    $class = array('star-no-view');
                }elseif($star == 5){
                    $class = array('star-view');
                }else{
                    for($i=0;$i<$star;$i++){
                        $class[] = 'star-view-one';
                    }
                    for($i=0;$i<(5-$star);$i++){
                        $class[] = 'star-no-view-one';
                    }
                }
                $arrDataUserReturn[]    =   array(
                                            'link'              =>      '/UsersNew/info?uid='.$itemUser['User']['id'],
                                            'avatar'            =>      $itemUser['User']['avata'],
                                            'full_name'         =>      $itemUser['User']['full_name'],
                                            'star_class'        =>      $class,
                                            'total_vote'        =>      $itemUser['User']['total_rate'],
                                            'total_comment'     =>      $itemUser['User']['total_comment'],
                                            'total_save'        =>      $itemUser['User']['total_save'],
                                            'stats_online'      =>      $itemUser['User']['stats_online'],
                                            'list_cat'          =>      @$arrUCatId[$itemUser['User']['id']],
                                            'date_reg_counse'   =>      date("d/m/Y G:i:s",$itemUser['User']['date_reg_counse'])
                                        );
            }
        }
        return array($arrDataUserReturn,$arrCatName);
    }
    function zendHtml($arrDataInfo){
        $htmlReturn = '';
        if(!empty($arrDataUserReturn)){
            foreach($arrDataUserReturn AS $itemUser){
                $htmlReturn .= '<div class="item-categ clearfix">
                                <p class="bottom imgs"><a href="'.$itemUser['link'].'"><img width="145" height="145" alt="Avatar" src="'.$itemUser['avatar'].'" /></a></p>
                                <div class="desc-item"><h4><a href="'.$itemUser['link'].'">'.$itemUser['full_name'].'</a></h4><p class="desc-text clearfix">';
                foreach($itemUser['star_class'] AS $itemStar){ 
                    $htmlReturn .= '<span class="'.$itemStar.'">&nbsp;</span>';
                }
                $htmlReturn .= '<span class="detail-star">
                                <b class="link-price">'.$itemUser['total_vote'].'</b> Bình chọn
                                <b class="link-price">'.$itemUser['total_comment'].'</b> Comment
                                <b class="link-price">'.$itemUser['total_save'].'</b> Save</span></p><div class="box-auction clearfix">';
                if($itemUser['stats_online'] == 1){
                    $htmlReturn .= '<span class="track_230" style=""><a title="Call" href="javascript:;" class="savethis" onclick="trackItem(230,364);" ondblclick="trackItem(230,364);"></a></span>';
                }
                $htmlReturn .= '<span class="timeleft">';
                if(!empty($itemUser['list_cat'])){
                    foreach($itemUser['list_cat'] AS $itemCat){
                        $htmlReturn .= '<span class="category-u">'.$arrCatName[$itemCat].'</span>';
                    }
                } 
                $htmlReturn.= '<br />Là thành viên tư vấn từ ngày '.$itemUser['date_reg_counse'].'</span><span class="checkout"></span></div></div></div>';  
            }
        }
        return $htmlReturn;
    }
    function getDefault($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'      =>  2,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  '*',
                                                'conditions'    =>  $arrCondition,
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star' => 'DESC','total_rate' => 'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    function newData($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'      =>  2,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  '*',
                                                'conditions'    =>  $arrCondition,
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('id'=>'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('id'=>'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    function totalSave($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'      =>  2,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  '*',
                                                'conditions'    =>  $arrCondition,
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_save'=>'DESC','total_rate' => 'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_save'=>'DESC','total_rate' => 'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    function getPayFree($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'          =>  2,
                        'consulting_fees'   =>  0,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id'                                             
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    function getPayTurn($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'              =>  2,
                        'turn_based_payment'    =>  1,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id'                                             
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    function getPayHr($idCat=0,$ofset=0){
        $arrCondition = array(
                        'group_id'      =>  2,
                        'paid_by_hour'  =>  1,
                        'stats_online'  =>  1
                    );
        if($idCat == 0)
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id'                                             
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        else{
            $arrCondition['UserCat.cat_id'] = $idCat;
            $arrUserInfo = $this->User->find('all',array(
                                                'fields'        =>  'User.*',
                                                'conditions'    =>  $arrCondition,
                                                'joins'         => array(
                                                    					array(
                                                    			            'table'      => 'user_cat',
                                                    			            'alias'      => 'UserCat',
                                                    			            'type'       => 'INNER',
                                                    			            'conditions' => array(
                                                                                    'User.id = UserCat.user_id',                 
                                                    			                )
                                                			              )
                                                			         ),
                                                'limit'         =>  AppConst::PER_PAGE_HOME,
                                                'group'         =>  array('User.id'),
                                                'offset'        =>  $ofset,
                                                'order'         =>  array('total_star'=>'DESC','total_rate' => 'DESC')
                                            ));
        }
        $totalData = sizeof($arrUserInfo);
        if($totalData < AppConst::PER_PAGE_HOME)
            $next = 0;
        else
            $next = 1;
        return  array(
                    'userInfo'  =>  $arrUserInfo,
                    'next'      =>  $next
                );
    }
    public function getCategory(){
    	$arrCategor = $this->Category->find('all');
    	return $arrCategor;
    }
}
?>